//
//  ProgressView.m
//  RssReader
//
//  Created by 曙光 王 on 12-5-12.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "ProgressView.h"
#import <QuartzCore/QuartzCore.h>

@implementation ProgressView
@synthesize progress;
@synthesize preTips;
@synthesize tips;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,frame.size.height)];
        tipsLabel.backgroundColor  = [UIColor clearColor];
        tipsLabel.font = [UIFont boldSystemFontOfSize:13.0f];
        tipsLabel.textAlignment = UITextAlignmentCenter;
        tipsLabel.textColor = [UIColor whiteColor];
        [self addSubview:tipsLabel];
        [tipsLabel release];
        
        //tipsLabel.text = @"正在加载";
        
        
        
        UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-108, frame.size.height-2, 108, 3)];
        loadingImageView.image = [UIImage imageNamed:@"progress_animation.png"];
        [self addSubview:loadingImageView];
        [loadingImageView release];
        
        [CATransaction begin];
        [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
        [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
        
        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
        [basicAnimation setFromValue:[NSNumber numberWithFloat:loadingImageView.center.x]];
        [basicAnimation setToValue:[NSNumber numberWithFloat:frame.size.width+loadingImageView.bounds.size.width/2]];
        [basicAnimation setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
        [basicAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
        [basicAnimation setRemovedOnCompletion:YES];
        [basicAnimation setRepeatCount:1e10f];
        [loadingImageView.layer addAnimation:basicAnimation forKey:@"loading"];
        
        [CATransaction commit];
        
        preTips = @"";
        tips = @"OK";
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect{
    // Drawing code
    CGRect allRect = CGRectMake(0, 0, self.bounds.size.width*self.progress, self.bounds.size.height);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //清除之前绘制的图形
    CGContextClearRect(context, allRect);
    
    //绘制进度
    CGContextSetRGBFillColor(context, 118.0f/255, 118.0f/255, 118.0f/255, 1);
    CGContextFillRect(context, self.bounds);
    CGContextSetRGBFillColor(context, 135.0f/255, 0.0f/255, 20.0f/255, 1.0f); // translucent white
    CGContextFillRect(context, allRect);
}

-(void) setTips:(NSString *)tip{
    [tips release];
    tips = tip;
    [tips retain];
    tipsLabel.text = [NSString stringWithFormat:@"%@(%@)",preTips,tip];
}

-(void) setPreTips:(NSString *)tip{
    [preTips release];
    preTips = tip;
    [preTips retain];
    tipsLabel.text = [NSString stringWithFormat:@"%@(%@)",tip,tips];
}

-(void) setProgress:(CGFloat)prog{
    progress = prog;
    [self setNeedsDisplay];
}

-(void) dealloc{
    [preTips release];
    [tips release];
    [super dealloc];
}

@end
